package cn.flightcloud.hi.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import cn.flightcloud.ServiceHiApplication;

@RestController
public class HiController {
	
	private static final Logger LOG = LoggerFactory.getLogger(ServiceHiApplication.class.getName());

	@Autowired
	private RestTemplate restTemplate;
	@Value("${server.port}")
	private String port;

	@RequestMapping("/hi")
	@HystrixCommand(fallbackMethod = "hiError")
	public String home(@RequestParam String name) {
//		throw new RuntimeException();
		return "hi " + name + ",i am from port:" + port;
	}

	public String hiError(String name) {
		return "hi," + name + ",sorry,error!";
	}
	
	@HystrixCommand(fallbackMethod = "hiError")
	@RequestMapping("/callHome")
	public String callHome() {
		LOG.info("calling trace service-hi  ");
		return restTemplate.getForObject("http://localhost:8989/miya", String.class);
	}

	public String hiError() {
		return "hi,sorry,error!";
	}

	@RequestMapping("/info")
	public String info() {
		LOG.info("calling trace service-hi ");
		return "i'm service-hi";
	}
}
